@{
    Layout = null;
}
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>生产排产</title>
    <link href="~/scripts/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />
    <script src="~/scripts/jquery-3.4.1.min.js"></script>
    <script src="~/scripts/layui-v2.5.6/layui/layui.js"></script>
    <style>
        body {
            margin: 0;
            padding: 20px;
            background: #f5f5f5;
            font-family: 'Microsoft YaHei', sans-serif;
        }
        .form-container {
            background: #fff;
            padding: 20px;
            border-radius: 4px;
            box-shadow: 0 2px 8px rgba(0,0,0,0.1);
        }
        .form-title {
            font-size: 18px;
            font-weight: bold;
            color: #333;
            margin-bottom: 20px;
            border-bottom: 2px solid #1890ff;
            padding-bottom: 10px;
        }
        .layui-form-item {
            margin-bottom: 20px;
        }
        .layui-form-label {
            width: 110px;
            font-weight: 500;
        }
        .layui-input-block {
            margin-left: 140px;
        }
        .form-actions {
            text-align: center;
            margin-top: 30px;
            padding-top: 20px;
            border-top: 1px solid #e8e8e8;
        }
        .form-actions .layui-btn {
            margin: 0 10px;
            min-width: 100px;
        }
        .required {
            color: #ff5722;
        }
    </style>
</head>
<body>
    <div class="form-container">
        <div class="form-title">
            <i class="layui-icon layui-icon-add-circle"></i> 生产排产
        </div>
        
        <form class="layui-form" id="schedulingForm" lay-filter="schedulingForm">
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md6">
                    <div class="layui-form-item">
                        <label class="layui-form-label"><span class="required">*</span>工单编码</label>
                        <div class="layui-input-block">
                            <input type="text" name="workOrderCode" lay-verify="required" placeholder="请输入工单编码" class="layui-input" autocomplete="off">
                        </div>
                    </div>
                </div>
                <div class="layui-col-md6">
                    <div class="layui-form-item">
                        <label class="layui-form-label"><span class="required">*</span>工单名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="workOrderName" lay-verify="required" placeholder="请输入工单名称" class="layui-input" autocomplete="off">
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md12">
                    <div class="layui-form-item">
                        <label class="layui-form-label">来源单据号</label>
                        <div class="layui-input-block">
                            <input type="text" name="sourceDocumentNo" placeholder="请输入来源单据号" class="layui-input" autocomplete="off">
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md6">
                    <div class="layui-form-item">
                        <label class="layui-form-label"><span class="required">*</span>产品编码</label>
                        <div class="layui-input-block">
                            <input type="text" name="productCode" lay-verify="required" placeholder="请输入产品编码" class="layui-input" autocomplete="off">
                        </div>
                    </div>
                </div>
                <div class="layui-col-md6">
                    <div class="layui-form-item">
                        <label class="layui-form-label">规格型号</label>
                        <div class="layui-input-block">
                            <input type="text" name="productModel" placeholder="请输入规格型号" class="layui-input" autocomplete="off">
                        </div>
                    </div>
                </div>
            </div>
            
            <div class="layui-row layui-col-space15">
                <div class="layui-col-md6">
                    <div class="layui-form-item">
                        <label class="layui-form-label">工序编码</label>
                        <div class="layui-input-block">
                            <input type="text" name="processCode" placeholder="请输入工序编码" class="layui-input" autocomplete="off">
                        </div>
                    </div>
                </div>
                <div class="layui-col-md6">
                    <div class="layui-form-item">
                        <label class="layui-form-label">工单数量</label>
                        <div class="layui-input-block">
                            <input type="number" name="workOrderQuantity" placeholder="请输入工单数量" class="layui-input" autocomplete="off" value="0">
                        </div>
                    </div>
                </div>
            </div>
            

            
            <div class="form-actions">
                <button class="layui-btn layui-btn-primary" type="button" id="cancelBtn">
                    <i class="layui-icon layui-icon-close"></i> 取消
                </button>
                <button class="layui-btn" lay-submit lay-filter="submitBtn">
                    <i class="layui-icon layui-icon-ok"></i> 确定
                </button>
            </div>
        </form>
    </div>

    <script>
        layui.use(['form', 'layer'], function() {
            var form = layui.form;
            var layer = layui.layer;
            
            // 自动生成工单编码
            function generateWorkOrderCode() {
                var now = new Date();
                var year = now.getFullYear();
                var month = String(now.getMonth() + 1).padStart(2, '0');
                var day = String(now.getDate()).padStart(2, '0');
                var random = String(Math.floor(Math.random() * 90000) + 10000);
                return 'MO' + year + month + day + random;
            }
            
            // 获取URL参数
            function getUrlParams() {
                var params = {};
                var urlSearchParams = new URLSearchParams(window.location.search);
                for (var pair of urlSearchParams.entries()) {
                    params[pair[0]] = pair[1];
                }
                return params;
            }
            
            // 页面加载时处理数据回显
            var urlParams = getUrlParams();
            
            // 如果URL中有工单数据，则回显；否则生成新的工单编码
            if (urlParams.workOrderCode) {
                // 使用传入的工单数据进行回显到现有表单字段
                $('input[name="workOrderCode"]').val(urlParams.workOrderCode || '');
                $('input[name="workOrderName"]').val(urlParams.workOrderName || '');
                $('input[name="productCode"]').val(urlParams.productCode || '');
                $('input[name="productModel"]').val(urlParams.productModel || '');
                
                // 来源单据号使用客户信息生成
                var sourceDoc = '';
                if (urlParams.customerCode) {
                    sourceDoc = '客户订单-' + urlParams.customerCode;
                }
                $('input[name="sourceDocumentNo"]').val(sourceDoc);
                
                // 工序编码使用传入的工序信息
                $('input[name="processCode"]').val(urlParams.processInfo || '主工序');
                
                // 工单数量使用传入的数量
                $('input[name="workOrderQuantity"]').val(urlParams.workOrderQuantity || '0');
            } else {
                // 新建排产时生成工单编码
                $('input[name="workOrderCode"]').val(generateWorkOrderCode());
            }
            
            // 取消按钮事件
            $('#cancelBtn').click(function() {
                var index = parent.layer.getFrameIndex(window.name);
                parent.layer.close(index);
            });
            
            // 表单提交事件
            form.on('submit(submitBtn)', function(data) {
                var formData = data.field;
                
                // 验证必填字段
                if (!formData.workOrderCode || !formData.workOrderName || !formData.productCode) {
                    layer.msg('请填写必填字段', {icon: 2});
                    return false;
                }
                
                // 构造符合实体的数据结构
                var schedulingData = {
                    workOrderCode: formData.workOrderCode,
                    workOrderName: formData.workOrderName,
                    sourceDocumentNo: formData.sourceDocumentNo || '',
                    productCode: formData.productCode,
                    productModel: formData.productModel || '',
                    processCode: formData.processCode || '',
                    workOrderQuantity: parseInt(formData.workOrderQuantity) || 0,
                    recordCreateTime: new Date().toISOString(),
                    recordUpdateTime: new Date().toISOString()
                };
                
                var loadIndex = layer.load(1, {
                    shade: [0.3, '#fff']
                });
                
                $.ajax({
                    url: '/ProductionScheduling/SaveScheduling',
                    type: 'POST',
                    contentType: 'application/json',
                    data: JSON.stringify(schedulingData),
                    success: function(response) {
                        layer.close(loadIndex);
                        if (response.success) {
                            layer.msg('保存成功', {icon: 1}, function() {
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                            });
                        } else {
                            layer.msg(response.message || '保存失败', {icon: 2});
                        }
                    },
                    error: function(xhr, status, error) {
                        layer.close(loadIndex);
                        console.error('保存失败:', error);
                        layer.msg('网络请求失败，请检查网络连接', {icon: 2});
                    }
                });
                
                return false; // 阻止表单默认提交
            });
        });
    </script>
</body>
</html> 